package com.liulin.cdse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liulin.cdse.entity.Manager;
import com.liulin.cdse.exception.AccountExistException;
import com.liulin.cdse.mapper.ManagerMapper;
import com.liulin.cdse.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public void add(Manager manager) {
        //判断管理员账号是否已经存在
        QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", manager.getAccount());

        Long count = managerMapper.selectCount(queryWrapper);
        //如果存在，抛出异常
        if (count > 0) {
            throw new AccountExistException("账号" + manager.getAccount() + "已存在");
        }

        //如果不存在，直接增加到表中
        managerMapper.insert(manager);
    }

    @Override
    public Manager login(String account, String password) {
        /*QueryWrapper<Manager> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", account);
        queryWrapper.eq("password", password);*/
        //封装查询条件
        LambdaQueryWrapper<Manager> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Manager::getAccount, account);
        queryWrapper.eq(Manager::getPassword, password);

        return managerMapper.selectOne(queryWrapper);
    }
}
